package bb.lanxing.view.sport;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.R;
import bb.lanxing.adapter.mine.SportTypeAdapter;
import bb.lanxing.util.DensityUtil;

public class SportTypeSwitch {
    private Context context;
    private int selectedSportType;

    private OnSportTypeSwitchListener listener;
    private PopupWindow popupWindow;

    private List<Integer> sportTypeList = new ArrayList();

    public SportTypeSwitch(Context context, int selectedSportType, int dataType) {
        this.context = context;
        this.selectedSportType = selectedSportType;
        initData(dataType);
        initView(context);
    }

    private void initData(int dataType) {
        sportTypeList = new ArrayList();
        if (dataType == 3 || dataType == 4) {
            sportTypeList.add(3);
            sportTypeList.add(2);
            sportTypeList.add(1);
        } else {
            sportTypeList.add(3);
            sportTypeList.add(2);
            sportTypeList.add(1);
            sportTypeList.add(6);
            sportTypeList.add(5);
            sportTypeList.add(8);
            sportTypeList.add(0);
        }
    }

    private void initView(final Context context) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.view_sport_type_switch, null);
        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(context, 3));
        SportTypeAdapter sportTypeAdapter = new SportTypeAdapter(sportTypeList, selectedSportType);
        sportTypeAdapter.setOnItemClickListener(position -> {
            listener.onSwitch(sportTypeList.get(position));
            if (popupWindow != null) {
                popupWindow.dismiss();
            }
        });
        recyclerView.setAdapter(sportTypeAdapter);
        popupWindow = new PopupWindow(inflate, -2, -2, true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setOnDismissListener(() -> {
            if (context instanceof Activity) {
                WindowManager.LayoutParams attributes = ((Activity) context).getWindow().getAttributes();
                attributes.alpha = 1.0f;
                ((Activity) context).getWindow().setAttributes(attributes);
            }
        });
    }

    public void showAsDropDown(View view) {
        if (popupWindow != null) {
            if (context instanceof Activity) {
                WindowManager.LayoutParams attributes = ((Activity) context).getWindow().getAttributes();
                attributes.alpha = 0.6f;
                ((Activity) context).getWindow().setAttributes(attributes);
            }
            popupWindow.showAsDropDown(view, 0, DensityUtil.dp2px(4.0f));
        }
    }

    public void setOnSportTypeSwitchListener(OnSportTypeSwitchListener onSportTypeSwitchListener) {
        listener = onSportTypeSwitchListener;
    }

    public interface OnSportTypeSwitchListener {
        void onSwitch(int i);
    }
}